/*
 * CarouselDemo.java
 *
 * Created on November 16, 2008, 4:26 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.sun.lwuit.uidemo;

import java.awt.Color;
import java.io.IOException;

import com.sun.dtv.lwuit.Button;
import com.sun.dtv.lwuit.Component;
import com.sun.dtv.lwuit.Container;
import com.sun.dtv.lwuit.Image;
import com.sun.dtv.lwuit.Label;
import com.sun.dtv.lwuit.List;
import com.sun.dtv.lwuit.layouts.BorderLayout;
import com.sun.dtv.lwuit.list.DefaultListCellRenderer;
import com.sun.dtv.lwuit.list.ListCellRenderer;

/**
 * 
 * @author tech
 */
public class CarouselDemo extends Demo {

	private String[] moviesIconsNames = { "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10", "w11" };// ,
	// "w12"};
	private String[] selectedMoviesIconsNames = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11" };// ,"s12"};

	/** Creates a new instance of CarouselDemo */
	public CarouselDemo() {
	}

	public String getName() {
		return "Carousel";
	}

	protected String getHelp() {
		return "Display carousel amination";
	}

	protected void execute(Container f) {
		f.setLayout(new BorderLayout());
		f.getStyle().setPadding(Component.LEFT, f.getComponentForm().getPreferredSize().getWidth() / 4);
		f.getStyle().setPadding(Component.RIGHT, f.getComponentForm().getPreferredSize().getWidth() / 4);
		f.getStyle().setPadding(Component.TOP, f.getComponentForm().getPreferredSize().getHeight() / 20);
		try {
			f.getStyle().setBgImage(Image.createImage("/bg2.png"));// .png"));
			f.getStyle().setScaleImage(false);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		List carousel = createCarousel();
		Button b;
		for (int i = 0; i < moviesIconsNames.length; i++) {
			try {
				Image img = Image.createImage("/" + moviesIconsNames[i] + ".jpg");
				// Added //TODO check
				// Image buttonIcon =
				// com.sun.lwuit.util.Effects.reflectionImage(img, 0.3f, 170);
				Image buttonIcon = img;

				// ////// Image selectedImg = Image.createImage("/" +
				// selectedMoviesIconsNames[i] + ".jpg");
				// Image selButtonIcon =
				// com.sun.lwuit.util.Effects.reflectionImage(selectedImg, 0.4f,
				// 100);
				b = new Button(buttonIcon);
				// ////// b.setRolloverIcon(selectedImg);//selButtonIcon);
				int scaledW = (img.getWidth() + img.getWidth() * 2);
				int scaledH = (img.getHeight() + img.getHeight() * 2);

				b.setRolloverIcon(img.scaled(scaledW, scaledH));
				// b.setRolloverIcon(null);
				carousel.addItem(b);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		f.addComponent(BorderLayout.CENTER, carousel);
	}

	private List createCarousel() {
		List l = new List();
		l.getStyle().setBgTransparency(0);

		ListCellRenderer renderer = new DefaultListCellRenderer() {

			public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
				Button b = (Button) value;
				getStyle().setBgTransparency(0);
				setFocus(true);
				if (isSelected) {
					try {
						// setIcon(com.sun.lwuit.util.Effects.reflectionImage(Image.createImage("/"
						// + selectedMoviesIconsNames[index] + ".jpg"), 0.4f,
						// 170)); //b.getRolloverIcon());
						// setIcon(Image.createImage("/" +
						// selectedMoviesIconsNames[index] + ".jpg"));
						// //b.getRolloverIcon());
						if (b.getRolloverIcon() == null) {
							// Added //TODO check
							// b.setRolloverIcon(com.sun.lwuit.util.Effects.reflectionImage(EncodedImage.createImage("/"
							// + selectedMoviesIconsNames[index] + ".jpg"),
							// 0.4f, 170));
							Image img = Image.createImage("/" + selectedMoviesIconsNames[index] + ".jpg");
							int scaledW = (img.getWidth() + img.getWidth() * 2);
							int scaledH = (img.getHeight() + img.getHeight() * 2);

							b.setRolloverIcon(img.scaled(scaledW, scaledH));
						}
						setIcon(b.getRolloverIcon());
					} catch (IOException ex) {
						ex.printStackTrace();
					}
				} else {
					setIcon(b.getIcon());
				}

				setAlignment(Label.HORIZONTAL_ALIGN_CENTER);
				getStyle().setPadding(0, 0, 0, 0);
				getStyle().setMargin(0, 0, 0, 0);
				// setPreferredSize(itemSize);
				return this;
			}

			public Component getListFocusComponent(List l) {
				getStyle().setBgSelectionColor(new Color(0x101010));
				// getStyle().setBgTransparency(165);
				setIcon(null);
				setFocus(true);
				return this;
			}
		};
		l.setOrientation(List.HORIZONTAL);
		l.getStyle().setBorder(null);
		l.setListCellRenderer(renderer);
		l.setFixedSelection(List.FIXED_LEAD); // TODO change to FIXED_CENTER
		// when available
		l.setItemGap(10);
		l.getStyle().setMargin(0, 0, 0, 0);
		l.getStyle().setPadding(0, 0, 0, 0);
		// l.setSmoothScrolling(false);
		return l;
	}
}
